import Taro from '@tarojs/taro'

export const showToast = title => {
  return Taro.showToast({
    title,
    icon: 'none',
    duration: 2000
  })
}

export const isPhone = phone => {
  const reg = /^1[3-9]\d{9}$/
  return reg.test(phone)
}

export const isName = name => {
  const reg = /^[\u4e00-\u9fa5]{1,10}$/
  return reg.test(name)
}

export const setUpdatePage = (num = 1) => {
  const pages = Taro.getCurrentPages()

  for (let i = 0; i < num; i++) {
    const page = pages[pages.length - 2 - i]

    page.setData({
      update: true
    })
  }
}

export const getUpdatePage = callback => {
  const pages = getCurrentPages()
  const curPage = pages[pages.length - 1]

  if (curPage.data.update) {
    callback()

    curPage.setData({
      update: false
    })
  }
}